﻿using System;
using System.Reflection;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace normalize_source_files
{
    class Program
    {
        static void NormalizeFile(string path, string encode)
        {
            File.WriteAllText(path, 
                File.ReadAllText(path, Encoding.GetEncoding(encode)).Replace("\0", ""), 
                Encoding.UTF8);
        }

        static void NormalizeFiles(string path, string pattern, string encode)
        {
            Console.WriteLine(path);

            if (Directory.Exists(path))
            {
                foreach (var file in Directory.GetFiles(path, pattern))
                    NormalizeFile(file, encode);

                foreach (var dir in Directory.GetDirectories(path))
                    NormalizeFiles(dir, pattern, encode);
            }
            else if (File.Exists(path))
                NormalizeFile(path, encode);
        }

        static void Main(string[] args)
        {
            var path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"..\..\data");
            var encode = "GB2312";

            if (args.Length > 0) path = args[0];
            if (args.Length > 1) encode = args[1];

            NormalizeFiles(path, "*.csv", encode);
        }
    }
}
